/**
 * sms_toy
 * 2012-3-1 2012
 * root
 * Copyright (c) 2012 Alex Wang
 */
package com.joyo8.app;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.TextView;

/**
 * @author root
 *
 */
public class SmsToSpeech extends Activity implements OnInitListener{
    TextToSpeech tts;
    LocationManager locMan ;
    LocationListener locLis;
    String speakContent = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_speech_sms);
        Constants.mUtil.logd("activity create");
        /*locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locLis = new MyLocationListener();
        locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10,
                locLis);*/
//        tts = new TextToSpeech(this,this);
        
        
       
    }
    public void onInit(int status) { 
        if(status == TextToSpeech.SUCCESS)  
        {
            tts.speak(speakContent, TextToSpeech.QUEUE_FLUSH, null);
        }else{
            Constants.mUtil.logd("tts is not ready");
        }
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
//        locMan.removeUpdates(locLis);

        tts.stop();
        if(tts != null){
            tts.shutdown();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if(Constants.mUtil.getGPSState(getApplicationContext())){
            Constants.mUtil.getLoction(getApplicationContext());
        }
        Intent intent = getIntent();
        
        TextView mTitle = (TextView)findViewById(R.id.txt_title);
        TextView mContent = (TextView)findViewById(R.id.txt_msg);
        
        if (intent.getStringExtra(Constants.EXTRA_SMS_CONTENT) != null) {
            speakContent = intent.getStringExtra(Constants.EXTRA_SMS_CONTENT);
            String sender = intent.getStringExtra(Constants.EXTRA_SMS_SENDER);
            mTitle.setText(String.format(getString(R.string.label_sms_title), sender));
            
            mContent.setText(speakContent);
            
            speakSms(speakContent);
        }
    }
    
    public void btnClick(View v){
        int id = v.getId();
        switch (id){
            case R.id.btn_close:
                finish();
                break;
            case R.id.btn_listen_again:
                speakSms(speakContent);
                break;
        }
    }
    
    private void speakSms(String content){
        if (tts == null) {
            tts = new TextToSpeech(this, this);
        } else {
            //tts.speak(speakContent, TextToSpeech.QUEUE_ADD, null);
            tts.speak(content, TextToSpeech.QUEUE_FLUSH, null);
        }
        
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }
    public class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location loc) {
            Constants.mUtil.logd("onLocationChanged");
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

}
